package com.atguigu.edu.controller;


import com.atguigu.edu.entity.Chapter;
import com.atguigu.edu.entity.vo.ChapterVideoVo;
import com.atguigu.edu.service.ChapterService;
import com.atguigu.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author admin
 * @since 2020-07-06
 */
@RestController
@RequestMapping("/edu/chapter")
@Api(description = "课程chapter-8001")
public class ChapterController {

    @Autowired
    ChapterService chapterService;

    @GetMapping("/getChapterVideoVo/{courseId}")
    @ApiOperation("根据ID查询 课程章节小结")
    public R getAll(@PathVariable String courseId) {
        List<ChapterVideoVo> chapterVideoList = chapterService.findChapterVideoById(courseId);
        return R.ok().data("chapterVideoList", chapterVideoList);
    }


    @PostMapping("/addChapter")
    @ApiOperation("添加课程章节")
    public R addChapter(@RequestBody Chapter chapter) {
        try {
            boolean b = chapterService.save(chapter);
            if (b) {
                return R.ok().message("添加成功!");
            } else {
                return R.error().message("添加失败!请检查数据信息!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok().message("添加成功!");
    }

    @ApiOperation("根据ID删除 课程章节")
    @DeleteMapping("/delChapter/{chapterId}")
    public R deleteChapter(@PathVariable String chapterId) {
        boolean b = chapterService.deleteChapterById(chapterId);
        if (b) {
            return R.ok().message("删除成功!");
        } else {
            return R.error();
        }

    }

    @PutMapping("/editChapter")
    @ApiOperation("修改课程章节")
    public R editChapter(@RequestBody Chapter chapter) {
        try {
            boolean b = chapterService.updateById(chapter);
            if (b) {
                return R.ok().message("修改成功!");
            } else {
                return R.error().message("修改失败!请检查数据信息!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error().message("修改失败!请检查数据信息!");
    }

    @ApiOperation("获取课程章节(根据chapterId)")
    @GetMapping("/getchapter/{chapterId}")
    public R getChapterByChapterId(@PathVariable String chapterId) {
        QueryWrapper<Chapter> queryWrapper = new QueryWrapper<>();
        try {
            queryWrapper.eq("id", chapterId);
            Chapter chapter = chapterService.getOne(queryWrapper);
            queryWrapper = null;
            return R.ok().data("chapter", chapter);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error();
    }
}

